cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _gameresult:null,
        _seats:[],
        _resultCont:null,
        _viewitemTemp:null
    },

    // use this for initialization
    onLoad: function () {
        console.log("MjGameResultOnLoad");
        if(cc.vv == null){
            return
        };
        // cc.vv.MjGameResult=this;
        this._gameresult = this.node.getChildByName("game_result");
        this._gameresult.active = false;
        // 分数
        this._resultCont=cc.find("Canvas/game_result/result/view/content");
        this._viewitemTemp=this._resultCont.children[0];
        this._resultCont.removeChild(this._viewitemTemp);
        var seats = this._gameresult.getChildByName("seats");
        for(var i = 0; i < seats.children.length; ++i){
            this._seats.push(seats.children[i].getComponent("Seat"))
        };
        var btnClose = cc.find("Canvas/game_result/btnClose");
        if(btnClose){
            cc.vv.utils.addClickEvent(btnClose,this.node,"MjGameResult","onBtnCloseClicked")
        };
        var btnQueding = cc.find("Canvas/game_result/btnQueding");
        if(btnQueding){
            cc.vv.utils.addClickEvent(btnQueding,this.node,"MjGameResult","onBtnCloseClicked")
        };
        var btnShare = cc.find("Canvas/game_result/btnShare");
        if(btnShare){
            cc.vv.utils.addClickEvent(btnShare,this.node,"MjGameResult","onBtnShareClicked")
        };
        //初始化网络事件监听器
        var self = this;
        this.node.on('game_end',function(){
            console.log("game_end");
            self.onGameEnd()
        })
    },
    onGameEnd:function(){
        var endinfo=cc.vv.gameNetMgr.endinfo;
        console.log("onGameEnd",endinfo);
        // 房号玩法显示
        var roominfo=this._gameresult.getChildByName("roominfo");
        roominfo.getComponent(cc.Label).string="房号:"+cc.vv.gameNetMgr.endRoomId;
        var wanfa=this._gameresult.getChildByName("wanfa");
        wanfa.getComponent(cc.Label).string="玩法:"+cc.vv.gameNetMgr.getWanfa();
        var seats = cc.vv.gameNetMgr.seats;
        var maxscore = 1;
        for(var i = 0; i < seats.length; ++i){
            var seat = seats[i];
            if(seat.score > maxscore){
                maxscore = seat.score;
            }
        };
        var myScore=seats[cc.vv.gameNetMgr.seatIndex].score;
        if (myScore>0) {
            cc.vv.audioMgr.playSFX("endwin.mp3")
        }else{
            cc.vv.audioMgr.playSFX("endlose.mp3")
        };
        for(var i = 0; i < seats.length; ++i){
            var seat = seats[i];
            this._seats[i].node.getChildByName("dayingjia").active=seat.score == maxscore;
            // 房主显示控制
            if (i==0) {
                this._seats[i]._isFangzhu=true
            }else{
                this._seats[i]._isFangzhu=false
            };
            this._seats[i].setInfo(seat.name,seat.score,true);
            // 自己和别人名字ID颜色不一样
            if (i==cc.vv.gameNetMgr.seatIndex) {
                this._seats[i].node.getChildByName("name").color=new cc.Color(187,77,22);
                this._seats[i].node.getChildByName("id").color=new cc.Color(187,77,22)
            }else{
                this._seats[i].node.getChildByName("name").color=new cc.Color(58,61,144);
                this._seats[i].node.getChildByName("id").color=new cc.Color(58,61,144)
            }
            this._seats[i].setID(seat.userid);
            // 根据正负分展示图片
            var scoreNode=this._seats[i].node.getChildByName("score");
                // 先清空
            scoreNode.removeAllChildren();
            var scoreArr=(seat.score+"").split("");
            console.log("scoreArr",scoreArr);
                // 正负号
            if (seat.score<0) {
                var spriteNode=new cc.Node("New Sprite");
                var sprite = spriteNode.addComponent(cc.Sprite);
                sprite.spriteFrame=cc.vv.mjmgr.getSpriteFrameByNum("-");
                scoreNode.addChild(spriteNode);
                scoreArr.splice(0,1)
            }else if(seat.score>0){
                var spriteNode=new cc.Node("New Sprite");
                var sprite = spriteNode.addComponent(cc.Sprite);
                sprite.spriteFrame=cc.vv.mjmgr.getSpriteFrameByNum("+");
                scoreNode.addChild(spriteNode)
            };

                // 具体分数
            for (var j = 0; j < scoreArr.length; j++) {
                var num=scoreArr[j];
                if (seat.score<=0) {
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getSpriteFrameByNum("-"+num);
                    scoreNode.addChild(spriteNode)
                }else{
                    var spriteNode=new cc.Node("New Sprite");
                    var sprite = spriteNode.addComponent(cc.Sprite);
                    sprite.spriteFrame=cc.vv.mjmgr.getSpriteFrameByNum(num);
                    scoreNode.addChild(spriteNode)
                }
            }
        };
        // 战绩列表
        this.initResultList(endinfo);
        // 是否俱乐部房间
        var clubNode=this._gameresult.getChildByName("club");
        if (cc.vv.gameNetMgr.clubid) {
            clubNode.active=true;
            clubNode.children[0].getComponent(cc.Label).string=cc.vv.gameNetMgr.clubname
        }else{
            clubNode.active=false
        };
        this._gameresult.active=true;
        this.doScale(this._gameresult)
    },
    // 战绩列表显示
    initResultList:function(endinfo){
        console.log("initResultList",endinfo);
        var data=endinfo[0].zanji;
        var jushuNum=["一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六"];
        for(var i = 0; i < data.length; ++i){
            // console.log("生成item");
            var node = this.getViewItem(i);
            var titleId = "第"+jushuNum[i]+"局";
            node.getChildByName("title").getComponent(cc.Label).string = titleId;
            // 分坐席写入
            for (var j = 0; j < endinfo.length; j++) {  
                console.log("每个坐席");
                // 分数
                var scoreNode=node.getChildByName("s"+j);
                var score=endinfo[j].zanji[i];
                scoreNode.getComponent(cc.Label).string = score;
                if (score>0) {
                    scoreNode.color=new cc.Color(255,222,0)
                }else if (score==0) {
                    scoreNode.color=new cc.Color(255,255,255)
                }else if (score<0) {
                    scoreNode.color=new cc.Color(0,255,240)
                };
                // 庄显示
                if (j==endinfo[j].zhuanglist[i]) {
                    node.getChildByName("z"+j).active=true
                }else{
                    node.getChildByName("z"+j).active=false
                }
            }
        }
    },
    // 生成内容框item
    getViewItem:function(index){
        var content = this._resultCont;
        if(content.childrenCount > index){
            return content.children[index]
        };
        var node = cc.instantiate(this._viewitemTemp);
        content.addChild(node);
        return node
    },
    // 从小到大放大效果(coco内置API放大动画会导致位移，这里因为有对齐会导致偏移出屏幕外)
    doScale:function(obj){
        // 全部挂在obj上作为属性避免定时器混乱
        obj.scaleX=0;
        obj.scaleY=0;
        obj.scaleNum=0;
        obj.scaleFn=function(){
            if (obj.scaleNum<10) {
                obj.scaleNum++;
                obj.scaleX=obj.scaleNum/10;
                obj.scaleY=obj.scaleNum/10;
                setTimeout(obj.scaleFn,20)
            }else{
                obj.scaleX=1;
                obj.scaleY=1
            }
        };
        obj.scaleFn()
    },
    onBtnCloseClicked:function(){
        cc.vv.gameNetMgr.roomId=null;
        cc.director.loadScene("mjhall")
    },
    
    onBtnShareClicked:function(){
        cc.vv.anysdkMgr.shareResult()
    }
});
